<?php

namespace baidu;


class Tts {
    const API_KEY = "Gapuoj4Bumc1tvzcykCzqLMU";
    const SECRET_KEY = "QGcti1ImWSHANPTx5fQlyrRDT5YGWuT4";

    public function shortCreate($text = '你好') {
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://tsn.baidu.com/text2audio",
            CURLOPT_TIMEOUT => 30,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER  => false,
            CURLOPT_SSL_VERIFYHOST  => false,
            CURLOPT_CUSTOMREQUEST => 'POST',

            CURLOPT_POSTFIELDS => 'tex=' . urlencode(urlencode($text)) . '&tok='. $this->getAccessToken() .'&cuid=jarpdwOMcaSUegbmchyip2XVT3Uj27XF&ctp=1&lan=zh&spd=5&pit=5&vol=5&per=1&aue=3',
//            CURLOPT_POSTFIELDS => 'tex=%E6%A0%B9%E6%8D%AE%E4%B8%8A%E5%9B%BE%E4%B8%AD%E7%9A%84%E7%BA%A2%E8%89%B2%E9%92%A9%2C%E9%80%89%E6%8B%A9%E2%80%9C%E9%9F%B3%E4%B9%90%E5%90%88%E5%B9%B6%E2%80%9D%E5%8A%9F%E8%83%BD%E3%80%82%20%E5%88%B0%E4%BA%86%E8%BF%99%E4%B8%AA%E5%90%88%E5%B9%B6%E7%9A%84%E7%95%8C%E9%9D%A2%E4%B8%8A%2C%E5%B0%B1%E5%8F%AF%E4%BB%A5%E7%9C%8B%E5%88%B0%E5%A4%A7%E4%BD%93%E7%9A%84%E5%8A%9F%E8%83%BD%E9%80%89%E9%A1%B9%E3%80%82%20%E7%82%B9%E5%87%BB%E4%B8%8A%E9%9D%A2%E7%9A%84%E2%80%9C%E6%B7%BB%E5%8A%A0%E2%80%9D%E6%8C%89%E9%92%AE%2C%E5%B0%B1%E5%8F%AF%E4%BB%A5%E6%8A%8A%E8%A6%81%E5%90%88%E5%B9%B6%E7%9A%84%E9%9F%B3%E9%A2%91%E6%96%87%E4%BB%B6%E5%8A%A0%E8%BD%BD%E5%88%B0%E8%BD%AF%E4%BB%B6%E4%B8%8A%2C%E5%8F%AF%E4%BB%A5%E5%90%8C%E6%97%B6%E5%A4%9A%E4%B8%AA%E9%80%89%E6%8B%A9(%E5%9C%A8%E8%BF%99%E9%87%8C%E6%88%91%E5%B0%B1%20%E6%A0%B9%E6%8D%AE%E4%B8%8A%E5%9B%BE%E4%B8%AD%E7%9A%84%E7%BA%A2%E8%89%B2%E9%92%A9%2C%E9%80%89%E6%8B%A9%E2%80%9C%E9%9F%B3%E4%B9%90%E5%90%88%E5%B9%B6%E2%80%9D&tok='. $this->getAccessToken() .'&cuid=jarpdwOMcaSUegbmchyip2XVT3Uj27XF&ctp=1&lan=zh&spd=5&pit=5&vol=5&per=1&aue=3',

            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/x-www-form-urlencoded',
                'Accept: */*'
            ),

        ));

        $response = curl_exec($curl);

        $info = curl_getinfo($curl);
        $error = curl_error($curl);
        curl_close($curl);


        if(array_key_exists('content_type',$info) &&  substr($info['content_type'], 0, 5) === "audio"){
            file_put_contents('./result_4.mp3', $response);
            return true;
        }else{
            return $response;
        }
    }

    public function create($text='你好') {
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://aip.baidubce.com/rpc/2.0/tts/v1/create?access_token={$this->getAccessToken()}",
            CURLOPT_TIMEOUT => 30,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER  => false,
            CURLOPT_SSL_VERIFYHOST  => false,
            CURLOPT_CUSTOMREQUEST => 'POST',

            CURLOPT_POSTFIELDS => json_encode([
                'lang'=>'zh',
                'text'=>$text
            ]),

            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json',
                'Accept: application/json'
            ),

        ));
        $response = curl_exec($curl);
        curl_close($curl);
        return $response;
    }

    public function query($task_ids) {
        if(!is_array($task_ids)) $task_ids = [$task_ids];

        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://aip.baidubce.com/rpc/2.0/tts/v1/query?access_token={$this->getAccessToken()}",
            CURLOPT_TIMEOUT => 30,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER  => false,
            CURLOPT_SSL_VERIFYHOST  => false,
            CURLOPT_CUSTOMREQUEST => 'POST',

            CURLOPT_POSTFIELDS => json_encode([
                'task_ids'=> $task_ids
            ]),

            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json',
                'Accept: application/json'
            ),
        ));
        $response = curl_exec($curl);
        curl_close($curl);
        return $response;
    }

    /**
     * 使用 AK，SK 生成鉴权签名（Access Token）
     * @return string 鉴权签名信息（Access Token）
     */
    private function getAccessToken(){
        $curl = curl_init();
        $postData = array(
            'grant_type' => 'client_credentials',
            'client_id' => self::API_KEY,
            'client_secret' => self::SECRET_KEY
        );
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://aip.baidubce.com/oauth/2.0/token',
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_SSL_VERIFYPEER  => false,
            CURLOPT_SSL_VERIFYHOST  => false,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POSTFIELDS => http_build_query($postData)
        ));
        $response = curl_exec($curl);
        curl_close($curl);
        $rtn = json_decode($response);
        return $rtn->access_token;
    }
}